package service.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import service.edu.exception.CustomException;
import service.edu.utils.JwtUtil;
import service.ucenter.entity.Member;
import service.ucenter.entity.vo.LoginMember;
import service.ucenter.entity.vo.RegisterMember;
import service.ucenter.mapper.MemberMapper;
import service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import service.edu.utils.Md5Util;

import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author diao
 * @since 2020-09-10
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(LoginMember loginMember) {
        //根据前端获取密码和手机号
        String phoneNum = loginMember.getMobile();
        String password = loginMember.getPassword();
        //判断前端传来数据是否为空
        if (StringUtils.isEmpty(phoneNum)||StringUtils.isEmpty(password)){throw new CustomException(20001,"登录失败");}
        //根据前端获得的手机号在数据库查找对应的用户
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", phoneNum);
        Member member = baseMapper.selectOne(wrapper);
        //如果查询不到则登录失败
        if (member==null){throw new CustomException(20001,"登录失败");}
        //如果查询到了,开始校验密码
        if(!Md5Util.encrypt(password).equals(member.getPassword())){throw new CustomException(20001,"密码错误");}
        //校验该用户是否被禁用
        if (member.getIsDisabled()){throw new CustomException(20001,"登录失败");}
        //返回token
        return JwtUtil.getJwtToken(member.getId(), member.getNickname());
    }

    //注册
    @Override
    public void register(RegisterMember registerMember) {
        //根据注册的数据获得手机号，密码等数据
        String phoneNum = registerMember.getMobile();//手机号
        String nickname = registerMember.getNickname();//昵称
        String password = registerMember.getPassword();//密码
        String code = registerMember.getCode();//验证码

        if (StringUtils.isEmpty(phoneNum)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)||StringUtils.isEmpty(code))
        {throw new CustomException(20001,"注册失败");}

        //校验验证码
        String redis_code = redisTemplate.opsForValue().get(phoneNum);
        if (!redis_code.equals(code)){throw new CustomException(20002,"验证码不正确");}
        //查询数据库中是否存在相同的手机号码
        /*
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", phoneNum);
        Member testPhoneNum = baseMapper.selectOne(wrapper);
        if (testPhoneNum!=null){throw new CustomException(20003,"已经注册");}
        */
        Integer count = baseMapper.selectCount(new QueryWrapper<Member>().eq("mobile", phoneNum));
        if (count>0){throw new CustomException(20003,"已经注册");}

        Member member = new Member();
        member.setMobile(phoneNum);
        member.setNickname(nickname);
        member.setPassword(Md5Util.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://edu-image111.oss-cn-beijing.aliyuncs.com/guli/avatar/lafei.jpg");
        member.setGmtCreate(new Date());
        member.setGmtModified(new Date());

        //将新增用户添加到数据库内 this.save(member);
        baseMapper.insert(member);
    }

    //统计一天之内注册的会员数
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegisterMembers(day);
    }
}
